package com.example.movephone.ui;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.movephone.Fragment.HomeFragment;
import com.example.movephone.Fragment.dongtaiFragment;
import com.example.movephone.Fragment.lianxiFragment;
import com.example.movephone.MainActivity;
import com.example.movephone.R;

import java.util.Calendar;
import java.util.Scanner;

public class HomepageActivity extends AppCompatActivity {
    //底部三个按钮
    private Button foot1;
    private Button foot2;
    private Button foot3;
    private ImageView image;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction=null;
    private HomeFragment homeFragment;
    private lianxiFragment lianxiFragment;
    private dongtaiFragment dongtaiFragment;
    private TextView text;

    /**
     * 消息界面布局
     */
    private View messageLayout;

    /**
     * 联系人界面布局
     */
    private View contactsLayout;

    /**
     * 动态界面布局
     */
    private View newsLayout;

    /**
     * 设置界面布局
     */
    private View settingLayout;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_homepage);
        fragmentManager=getSupportFragmentManager();
        init();
        //setTabSelection(0);
        //点击事件
        onclick();
    }
    /**
     * 根据传入的index参数来设置选中的tab页。
     *
     * @param index
     * 每个tab页对应的下标。0表示消息，1表示联系人，2表示动态，3表示设置。
     */
   /* private void setTabSelection(int index) {
        // 每次选中之前先清楚掉上次的选中状态
        clearSelection();
        // 开启一个Fragment事务
        FragmentTransaction transaction = fragmentManager.beginTransaction();
        // 先隐藏掉所有的Fragment，以防止有多个Fragment显示在界面上的情况
        hideFragments(transaction);
        switch (index) {
            case 0:
                messageLayout.setBackgroundColor(0xff0000ff);
                if (homeFragment == null) {
                    // 如果homeFragment为空，则创建一个并添加到界面上
                    homeFragment = new HomeFragment();
                    transaction.add(R.id.content, homeFragment);
                } else {
                    // 如果homeFragment不为空，则直接将它显示出来
                    transaction.show(homeFragment);
                }
                break;
            case 1:
                // 当点击了联系人tab时，改变控件的图片和文字颜色
                contactsLayout.setBackgroundColor(0xff0000ff);
                if (lianxiFragment == null) {
                    // 如果lianxiFragment为空，则创建一个并添加到界面上
                    lianxiFragment = new lianxiFragment();
                    transaction.add(R.id.content, lianxiFragment);
                } else {
                    // 如果lianxiFragment不为空，则直接将它显示出来
                    transaction.show(lianxiFragment);
                }
                break;
            case 2:
                // 当点击了动态tab时，改变控件的图片和文字颜色
                newsLayout.setBackgroundColor(0xff0000ff);
                if (dongtaiFragment == null) {
                    // 如果dongtaiFragment为空，则创建一个并添加到界面上
                    dongtaiFragment = new dongtaiFragment();
                    transaction.add(R.id.content, dongtaiFragment);
                } else {
                    // 如果dongtaiFragment不为空，则直接将它显示出来
                    transaction.show(dongtaiFragment);
                }
                break;
            default:
        }
        transaction.commit();
    }*/

    /**
     * 将所有的Fragment都置为隐藏状态。
     *
     * @param transaction
     * 用于对Fragment执行操作的事务
     */
 /*   private void hideFragments(FragmentTransaction transaction) {
        if (homeFragment != null) {
            transaction.hide(homeFragment);
        }
        if (lianxiFragment != null) {
            transaction.hide(lianxiFragment);
        }
        if (dongtaiFragment != null) {
            transaction.hide(dongtaiFragment);
        }
    }*/

    /**
     * 清除掉所有的选中状态。
     */
    private void clearSelection() {
        messageLayout.setBackgroundColor(0xffffffff);
        contactsLayout.setBackgroundColor(0xffffffff);
        newsLayout.setBackgroundColor(0xffffffff);
        settingLayout.setBackgroundColor(0xffffffff);
    }
    //点击事件
    private void onclick() {
        foot1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=fragmentManager.beginTransaction();
                homeFragment=new HomeFragment();
                transaction.replace(R.id.fragment_tv,homeFragment);
                transaction.commit();
            }
        });
        foot2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=fragmentManager.beginTransaction();
                lianxiFragment=new lianxiFragment();
                transaction.replace(R.id.fragment_tv,lianxiFragment);
                transaction.commit();
            }
        });
        foot3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                transaction=fragmentManager.beginTransaction();
                dongtaiFragment=new dongtaiFragment();
                transaction.replace(R.id.fragment_tv,dongtaiFragment);
                transaction.commit();
            }
        });
        image.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showDatePickDlg();
            }
        });

    }

    //日历
    private void showDatePickDlg() {
        Calendar calendar=Calendar.getInstance();
        DatePickerDialog dialog=new DatePickerDialog(HomepageActivity.this, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker datePicker, int year, int monthOfYear, int dayOfMonth) {
                HomepageActivity.this.text.setText(year+ "年" + monthOfYear + "月" + dayOfMonth+"日");
            }
        }, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH)+1, calendar.get(Calendar.DAY_OF_MONTH));
        dialog.show();
    }

    //初始化
    private void init() {
        foot1 =findViewById(R.id.bt1);
        foot2 =findViewById(R.id.bt2);
        foot3 =findViewById(R.id.bt3);
        image=findViewById(R.id.image);
        text=findViewById(R.id.text);
    }
}